package service

import (
	"context"
	"go.opentelemetry.io/otel"
	"userrpc/rpc"

	"github.com/cloudwego/hertz/pkg/app"
	user "userapi/hertz_gen/user"
	rpcmodel "userrpc/kitex_gen/user"
)

type LoginService struct {
	RequestContext *app.RequestContext
	Context        context.Context
}

func NewLoginService(Context context.Context, RequestContext *app.RequestContext) *LoginService {
	return &LoginService{RequestContext: RequestContext, Context: Context}
}

func (h *LoginService) Run(req *user.User) (resp *user.User, err error) {
	// 链路追踪
	spanCtx, span := otel.Tracer("login").Start(h.Context, "loginRpc")
	r, err := rpc.Client().Login(spanCtx, &rpcmodel.User{})
	span.End()
	if err != nil {
		return nil, err
	}
	resp = &user.User{
		ID:         r.Id,
		Username:   r.Username,
		Role:       r.Role,
		Phone:      r.Phone,
		CreateTime: r.CreateTime,
		DeleteTime: r.DeleteTime,
		Status:     r.Status,
		Password:   r.Password,
	}
	return
}
